import time
import pygame
import random

# 创建游戏窗口大小
WIDTH = 360
HEIGHT = 480
FPS = 40
# Colors(R,G,B)
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)


# 精灵
class Player(pygame.sprite.Sprite):
    # sprite for the player
    def __init__(self):
        # this line is required to properly create the sprite
        pygame.sprite.Sprite.__init__(self)
        # creat a plain rectangle for the sprite image
        self.image = pygame.Surface((50, 50))
        self.image.fill(GREEN)
        #find the rectangle that encloses the image
        self.rect=self.image.get_rect()
        #center the sprite on the screen
        self.rect.center=(WIDTH/2,HEIGHT/2)
    def update(self):
        # any code here will happen every time the game loop updates
        self.rect.x+=3
        if self.rect.left>WIDTH:
            self.rect.right=0

# creat window
pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("MY Game")
clock = pygame.time.Clock()

myfont = pygame.font.Font(None, 60)
textImage = myfont.render('pygame', True, WHITE)
# game loop
running = True
count = 0
start = time.time()
while running:
    # keep right speed
    clock.tick(FPS)
    # input
    for event in pygame.event.get():
        # check for closing window
        if event.type == pygame.QUIT:
            running = False
    # update
    count += 1
    now = time.time()
    fps = count / (now - start)
    fpsImage = myfont.render(str(fps), True, WHITE)
    # draw/render
    screen.fill(BLACK)
    screen.blit(fpsImage, (10, 100))
    pygame.display.flip()
pygame.quit